<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>驿站管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索行 -->
    <el-form :inline="true" :model="yzqueryInfo" class="demo-form-inline">
      <el-form-item label="驿站名称:">
        <el-input
          v-model="yzqueryInfo.courierStationName"
          placeholder="驿站名称"
          @clear="clearInput"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item label="小区名称:">
        <el-input
          clearable
          @clear="clearInput"
          v-model="yzqueryInfo.communityName"
          placeholder="小区名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="提交日期:">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="datevalue"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="stationList">搜索</el-button>
      </el-form-item>
    </el-form>

    <!-- 添加驿站按钮 -->
    <el-row>
      <el-button type="primary" @click="tianjia">添加驿站</el-button>
    </el-row>

    <el-table :data="stationlist" style="width: 100%">
      <el-table-column prop="courierStationId" label="ID" width="150">
      </el-table-column>
      <el-table-column prop="courierStationName" label="驿站名字" width="150">
      </el-table-column>
      <el-table-column
        prop="courierStationProvince"
        label="驿站地址"
        width="150"
      >
      </el-table-column>
      <el-table-column
        prop="courierStationCommunityName"
        label="所在小区"
        width="150"
      >
      </el-table-column>
      <el-table-column
        prop="courierStationOrderCount"
        label="营订单总数"
        width="150"
      >
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" width="150">
      </el-table-column>
      <el-table-column label="状态" width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.courierStationStatus == 1"
            type="success"
            disabled
            >上架中</el-button
          >
          <el-button
            v-if="scope.row.courierStationStatus == 0"
            type="danger"
            disabled
            >下架中</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.courierStationStatus == 1"
            type="text"
            @click="shangjia(scope.row)"
            >立即下架</el-button
          >
          <el-button
            v-if="scope.row.courierStationStatus == 0"
            type="text"
            @click="shangjia(scope.row)"
            >立即上架</el-button
          >
          <el-row>
            <el-button type="text" @click="updataYZ(scope.row)"
              >编辑</el-button
            ></el-row
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="yzqueryInfo.pageNum"
      :page-size="yzqueryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :total="newtotal"
    >
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog
      :title="biaotou"
      :visible.sync="dialogFormVisible"
      @close="quxiao"
    >
      <el-form :model="yizhanData">
        <el-form-item label="驿站名字" label-width="100px">
          <el-input
            style="width: 220px"
            v-model="yizhanData.courierStationName"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="所属地区" label-width="100px">
          <el-cascader v-model="address" :options="citys"></el-cascader>
        </el-form-item>

        <el-form-item label-width="100px">
          <el-input
            v-model="yizhanData.courierStationAddress"
            placeholder="请输入详细地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择小区" label-width="100px">
          <el-select
            v-model="yizhanData.courierStationCommunityId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in communityList2"
              :key="item.communityId"
              :label="item.communityName"
              :value="item.communityId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取消</el-button>
        <el-button type="primary" @click="baocun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script scoped>
import citys from "../assets/citydata.js";
export default {
  data() {
    return {
      queryInfo: {
        communityName: "",
        startTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
      },
      communityList2: [],

      address: [],
      citys: citys,
      dialogFormVisible: false,
      yizhanData: {
        courierStationAddress: "",
        courierStationCity: "",
        courierStationCommunityId: "",
        courierStationDistrict: "",
        courierStationImage: "",
        courierStationName: "",
        courierStationProvince: "",
      },
      biaotou: "添加驿站",
      sjStatus: "",
      yzqueryInfo: {
        pageNum: 1,
        pageSize: 10,
        communityName: "",
        courierStationName: "",
        courierStationStatus:null,
        startTime: "",
        endTime: "",
      },
      newtotal: 1,
      datevalue: "",
      stationlist: [],
      adress: "",
    };
  },
  methods: {
    shangjia(row) {
      this.$http
        .post("courierStation/modifyCourierStation", {
          courierStationId:row.courierStationId,
          courierStationStatus: (row.courierStationStatus==0)? 1:0,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.message);
            this.stationList();
          }
        })
        .catch((error) => {
          this.$message.error("请求错误");
        });
    },
    quxiao() {
      this.dialogFormVisible = false;
      this.address = [];
      this.yizhanData = {
        courierStationAddress: "",
        courierStationCity: "",
        courierStationCommunityId: "",
        courierStationDistrict: "",
        courierStationImage: "",
        courierStationName: "",
        courierStationProvince: "",
      };
      this.stationList();
    },
    // 更新驿站信息
    updataYZ(info) {
      this.dialogFormVisible = true;
      this.biaotou = "编辑驿站";
      this.yizhanData = info;
      this.address = [
        this.yizhanData.courierStationProvince,
        this.yizhanData.courierStationCity,
        this.yizhanData.courierStationDistrict,
      ];
    },
    //保存
    baocun() {
      if (this.biaotou === "添加驿站") {
        this.$http
          .post("courierStation/addCourierStation", this.yizhanData)
          .then((res) => {
            if (res.data.status == 200) {
              this.$message.success(res.data.message);
              this.dialogFormVisible = false;
              this.stationList();
            }
          })
          .catch((error) => {
            this.$message.error("请求错误");
          });
      }
      if (this.biaotou === "编辑驿站") {
        this.$http
          .post("courierStation/modifyCourierStation", this.yizhanData)
          .then((res) => {
            if (res.data.status == 200) {
              this.$message.success(res.data.message);
              this.dialogFormVisible = false;
              this.stationList();
            }
          })
          .catch((error) => {
            this.$message.error("请求错误");
          });
      }
    },
    communityList() {
      this.$http
        .get("community/communityList", {
          params: this.queryInfo,
        })
        .then((respond) => {
          if (respond.data.status == 200) {
            this.communityList2 = respond.data.data.list;
          } else {
            this.$message.error("回应错了");
          }
        })
        .catch((error) => {
          this.$message.error("请求错误");
        });
    },
    //添加按钮
    tianjia() {
      this.dialogFormVisible = true;
      this.biaotou = "添加驿站";
    },
    clearInput() {
      this.stationList();
    },

    stationList() {
      this.$http
        .get("courierStation/querySearchCourierStationList", {
          params: this.yzqueryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.stationlist = res.data.data.list;
            this.newtotal = res.data.data.total;
          } else {
            this.$message.error("请求错误");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleSizeChange(val) {
      this.yzqueryInfo.pageSize = val;
      this.yzqueryInfo.pageNum = 1;
      this.stationList();
    },
    handleCurrentChange(val) {
      this.yzqueryInfo.pageNum = val;
      this.stationList();
    },
  },

  mounted() {
    this.stationList();
    this.communityList();
  },
  watch: {
    datevalue() {
      this.yzqueryInfo.startTime = this.datevalue[0];
      this.yzqueryInfo.endTime = this.datevalue[1];
    },
    address() {
      this.yizhanData.courierStationProvince = this.address[0];
      this.yizhanData.courierStationCity = this.address[1];
      this.yizhanData.courierStationDistrict = this.address[2];
    },
  },
};
</script>
  
  <style>
</style>